package com.jh.tools.update.app;

import android.app.IntentService;
import android.content.Intent;
import android.net.Uri;
import android.os.Handler;
import android.support.v4.app.NotificationCompat.Builder;

import com.jh.tools.common.notify.NotifyManage;
import com.nostra13.universalimageloader.utils.StorageUtils;

import java.io.File;

/**
 * 应用下载服务类
 */
public class DownloadService extends IntentService {
    // 10-10 19:14:32.618: D/DownloadService(1926): 测试缓存：41234 32kb
    // 10-10 19:16:10.892: D/DownloadService(2069): 测试缓存：41170 1kb
    // 10-10 19:18:21.352: D/DownloadService(2253): 测试缓存：39899 10kb
    private static final String TAG = "DownloadService";
    private Builder mBuilder;
    private static final int DOWNLOAD_APP = 0;
    private static final int Download_file = 1;
    private static final int Downloading = 4;
    private static final int Download_err = 4;
    File apkFile;
    public String urlStr;//下载地址
    public String fileName;//文件名
    public String filetype;//文件类型
    public String notifyId;//notifyId
    public boolean DOWNLOAD_SILENT;//是否静默下载
    private DownloadUtil mDownloadUtil;

    public DownloadService() {
        super("DownloadService");
        mDownloadUtil = new DownloadUtil();
    }

    @Override
    protected void onHandleIntent(Intent intent) {

        mBuilder = new Builder(this);
        urlStr = intent.getStringExtra(UPConstants.APK_DOWNLOAD_URL);
        fileName = intent.getStringExtra(UPConstants.DOWNLOAD_FILE_NAME);
        filetype = intent.getStringExtra(UPConstants.NOTIFYID_UPDATE_PKGNAME);
        notifyId = intent.getStringExtra(UPConstants.NOTIFYID_UPDATE_PKGNAME);
        DOWNLOAD_SILENT = intent.getBooleanExtra(UPConstants.DOWNLOAD_SILENT, false);
        if (DOWNLOAD_SILENT == true) {
            mDownloadUtil.Download(urlStr, apkFile, h);
        } else {
            File dir = StorageUtils.getCacheDirectory(this);
            apkFile = new File(dir, fileName);
            if (!apkFile.exists()) {
                mDownloadUtil.Download(urlStr, apkFile, h);
            }
            if (DownloadType.DOWNLOAD_APP_SUC == Integer.parseInt(filetype)) {
                h.hasMessages(DownloadType.DOWNLOAD_APP_SUC);
            } else {
                h.hasMessages(DownloadType.DOWNLOAD_FILE_SUC);
            }
        }
    }

    Handler h = new Handler() {
        public void handleMessage(android.os.Message msg) {
            switch (msg.what) {
                case DownloadType.DOWNLOAD_APP_SUC://app下载完成,进行安装
                    if (DOWNLOAD_SILENT == false) {
                        NotifyManage.getInstance().notify_progress_update(notifyId, 100, 100, apkFile);
                        Uri uri = Uri.fromFile(apkFile);
                        Intent intent = new Intent(Intent.ACTION_VIEW);
                        intent.setDataAndType(uri, "application/vnd.android.package-archive");
                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        startActivity(intent);
                    }
                    stopSelf();
                    break;
                case DownloadType.DOWNLOAD_FILE_SUC://文件下载完成
                    if (DOWNLOAD_SILENT == false) {

                    }
                    stopSelf();
                    break;
                case DownloadType.DOWNLOADING://下载中
                    if (DOWNLOAD_SILENT == false) {
                        NotifyManage.getInstance().notify_progress_update(notifyId, 100, msg.arg1, apkFile);
                    }
                    break;
                case DownloadType.DOWNLOAD_ERR://下载失败
                    if (DOWNLOAD_SILENT == false) {

                    }
                    stopSelf();
                    break;
                default:
                    break;
            }

        }

        ;
    };


}
